package cn.tx;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;

class Myframe extends JFrame {
	JLabel lab1, lab2, lab3;
	JTextField txt1, txt2;
	JButton bun1, bun2;
	Font f1;

	public Myframe(String str) {
		super(str);
	}

	void inint() {
		this.setLayout(null);
		this.setBounds(450, 200, 500, 400);
		lab1 = new JLabel("用户名:");
		lab2 = new JLabel("密码:");
		lab3 = new JLabel("ͬ同学录");
		bun1 = new JButton("确定");
		bun2 = new JButton("取消");
		txt1 = new JTextField();
		txt2 = new JTextField();

		lab1.setFont(new Font("宋体", Font.PLAIN, 17));
		lab2.setFont(new Font("宋体", Font.PLAIN, 17));
		lab3.setFont(new Font("宋体", Font.PLAIN, 17));
		bun1.setFont(new Font("宋体", Font.PLAIN, 16));
		bun2.setFont(new Font("宋体", Font.PLAIN, 16));

		lab1.setBounds(160, 100, 60, 30);
		lab2.setBounds(160, 130, 80, 30);
		lab3.setBounds(190, 30, 120, 30);
		txt1.setBounds(220, 100, 100, 25);
		txt2.setBounds(220, 130, 100, 25);
		bun1.setBounds(150, 200, 70, 30);
		bun2.setBounds(280, 200, 70, 30);

		this.add(lab1);
		this.add(lab2);
		this.add(lab3);
		this.add(txt1);
		this.add(txt2);
		this.add(bun1);
		this.add(bun2);

		/**
		 * 添加
		 */
		bun1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int i1 = 0;
				try {
					java.sql.Statement stmt = null;
					java.sql.Connection conn = null;
					Class.forName("com.mysql.jdbc.Driver");
					conn = DriverManager.getConnection(
				"jdbc:mysql://localhost:3306/test", "root", "123456");

					stmt = conn.createStatement();
					ResultSet rs1 = stmt.executeQuery("select * from stu1");
					while (rs1.next()) {
						String s11 = rs1.getString("id");
						String s22 = rs1.getString("password");
						if (txt1.getText().equals(s11) && txt2.getText().equals(s22)) {
							i1 = 1;
							break;
						}
					}
					if (txt1.getText().equals("") || txt2.getText().equals("")) {
						i1 = 2;
					}
					if (i1 == 0) {
						JOptionPane.showMessageDialog(bun1, "用户名或者密码输入错误", "��ʾ", JOptionPane.WARNING_MESSAGE);
					} else if (i1 == 1) {
						try {
							new Main();
						} catch (Exception ex) {
							ex.getMessage();
						}
						Myframe.this.setVisible(false);
						
						
						//JOptionPane.showMessageDialog(bun1, "     ������ȷ", "��ϲ", JOptionPane.WARNING_MESSAGE);
					} else {
						JOptionPane.showMessageDialog(bun1, "输入不能为空", "��ʾ", JOptionPane.WARNING_MESSAGE);
					}
				} catch (Exception e1) {
					// Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);

	}

}

